package easydevelop.popup.actions;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import easydevelop.EasydevelopPlugin;

public class GetResourceAction implements IObjectActionDelegate {

    protected static Clipboard sysc = null;

    protected static String GET_PATH_ACTION_ID = null;

    protected static String GET_CLASS_ACTION_ID = null;

    protected static String LINE_SEP = "\n";

    protected Object[] currentResources = null;

    protected TreeMap prjPathMap = new TreeMap();

    static{
        sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public GetResourceAction() {        
        GET_PATH_ACTION_ID =
            EasydevelopPlugin.getResourceString("getpath.action.id");
        GET_CLASS_ACTION_ID =
            EasydevelopPlugin.getResourceString("getclass.action.id");

    }

    public void setActivePart(IAction action, IWorkbenchPart targetPart) {

    }

    public void run(IAction action) {
        if (currentResources == null || currentResources.length == 0) {
            return;
        }
        StringBuffer buf = new StringBuffer();
        if (GET_PATH_ACTION_ID.equals(action.getId())) {

            for (int i = 0; i < currentResources.length; i++) {
                if (currentResources[i] instanceof IFile) {
                    IFile tmpFile = (IFile) currentResources[i];
                    buf.append(tmpFile.getFullPath().toString());
                    buf.append(LINE_SEP);
                }
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            sysc.setContents(new StringSelection(buf.toString()), null);
        } else if (GET_CLASS_ACTION_ID.equals(action.getId())) {
            IFile tmpFile = (IFile) currentResources[0];
            IJavaProject javaPrj = JavaCore.create(tmpFile.getProject());
            String prjPath = javaPrj.getPath().toString(); //"/easydevelop"
            ArrayList packageRootLst = null;
            if(this.prjPathMap.containsKey(prjPath)) {
                packageRootLst = (ArrayList) this.prjPathMap.get(prjPath);
            } else {
                packageRootLst = new ArrayList();
                this.prjPathMap.put(prjPath, packageRootLst);
                IPackageFragmentRoot[] packageRoots = null;
                try {
                    packageRoots = javaPrj.getAllPackageFragmentRoots();
                } catch (JavaModelException e) {
                    e.printStackTrace();
                }
                if (packageRoots == null || packageRoots.length == 0) {
                    return;
                }
                for (int i = 0; i < packageRoots.length; i++) {
                    String packageRootsPath = packageRoots[i].getPath().toString();
                    if (packageRootsPath.startsWith(prjPath)) {
                        packageRootLst.add(packageRootsPath);
                    }
                }
            }

            for (int i = 0; i < currentResources.length; i++) {
                if (currentResources[i] instanceof IFile) {
                    tmpFile = (IFile) currentResources[i];
                    String fileIPath = tmpFile.getFullPath().toString();
                    String targetPath = null;
                    Iterator itLst = packageRootLst.iterator();
                    while (itLst.hasNext()) {
                        String tmp = (String) itLst.next();
                        if (fileIPath.startsWith(tmp)) {
                            targetPath = fileIPath.substring(tmp.length() + 1);
                        }
                    }
                    if (targetPath != null) {
                        buf.append(targetPath.replace('/', '.') + "\n");
                    }
                }
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            sysc.setContents(new StringSelection(buf.toString()), null);
        }
    }

    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof StructuredSelection) {
            StructuredSelection ss = (StructuredSelection) selection;
            this.currentResources = ss.toArray();
        }
    }

}
